package com.huayue.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.Employee;
import com.huayue.entity.User;
import com.huayue.service.EmployeeService;
import com.huayue.service.UserService;
import com.huayue.util.FileUploadHelper;
import com.huayue.util.PageHelper;

/**
 * 员工
 * 
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService svc;
    @Autowired
    UserService userSvc;

    BaseController bc = new BaseController();

    /**
     * 加载下拉框中的内容
     * 
     * @return
     */
    @RequestMapping("/loadSelect")
    public ModelAndView loadSelect() {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("data_dep", svc.findAllDep());
        view.addStaticAttribute("data_role", svc.findAllRole());
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "添加-员工信息")
    @RequestMapping("/add")
    public ModelAndView add(Employee employee) {
        return bc.add(svc, employee);
    }

    @LogAnno(operateType = "修改-员工信息")
    @RequestMapping("/update")
    public ModelAndView update(Employee employee) {
        return bc.update(svc, employee);
    }

    @LogAnno(operateType = "删除-员工信息")
    @RequestMapping("/delete")
    public ModelAndView delete(Employee employee) {
        return bc.delete(svc, employee);
    }

    @RequestMapping("/findOne")
    public ModelAndView findOne(Employee employee) {
        return bc.findOne(svc, employee);
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, Employee employee) {
        return bc.findPage(svc, page, employee, employee);
    }

    /**
     * 根据手机号查找用户信息
     *
     * @param employee
     * @return
     */
    @RequestMapping("/findEmpByPhone")
    public ModelAndView findEmpByPhone(Employee employee) {
        FastJsonJsonView view = new FastJsonJsonView();
        Map<String, Object> map = svc.findOneByPhone(employee);
        if (map != null) {
            view.addStaticAttribute("status", 300);
            view.addStaticAttribute("msg", "已存在该账号");
            view.addStaticAttribute("data", map);
        } else {
            view.addStaticAttribute("status", 200);
            view.addStaticAttribute("msg", "可用");
        }
        return new ModelAndView(view);
    }

    /**
     * 已完成登录后更改密码
     *
     * @param oldPassword,password,repassword
     * @return
     */
    @LogAnno(operateType = "已登陆-修改密码")
    @RequestMapping("/updatePasswordNormal")
    public ModelAndView updatePasswordNormal(String oldPassword, String password, String repassword,
        HttpServletRequest request) {
        FastJsonJsonView view = new FastJsonJsonView();
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        int status = 0;

        if (user.getPhone() != null && !"".equals(user.getPhone())) {
            if (oldPassword.equals(user.getPassword())) {
                if (!user.getPassword().equals(password)) {
                    user.setPassword(password);
                    if (password.equals(repassword)) {
                        status = userSvc.updatePassword(user);
                        if (status > 0) {
                            view.addStaticAttribute("icon", 1);
                            view.addStaticAttribute("msg", "密码修改成功！请使用新密码进行登录");
                        } else {
                            view.addStaticAttribute("icon", 2);
                            view.addStaticAttribute("msg", "密码修改失败！");
                        }
                    } else {
                        view.addStaticAttribute("icon", 2);
                        view.addStaticAttribute("msg", "请确认您的新密码两次输入是否相同！");
                    }
                } else {
                    view.addStaticAttribute("icon", 2);
                    view.addStaticAttribute("msg", "新密码与相同！");
                }
            } else {
                view.addStaticAttribute("icon", 2);
                view.addStaticAttribute("msg", "请输入正确的当前密码！");
            }
        } else {
            view.addStaticAttribute("icon", 2);
            view.addStaticAttribute("msg", "未找到您的信息，请尝试重新登录！");
        }
        view.addStaticAttribute("code", 0);
        view.addStaticAttribute("status", status);
        return new ModelAndView(view);
    }

    /**
     * 上传头像
     * 
     * @param request
     * @return
     */
    @LogAnno(operateType = "文件上传-头像")
    @RequestMapping(value = "/uploadIcon", method = RequestMethod.POST)
    public ModelAndView uploadIcon(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        String fileName = FileUploadHelper.fileUpload(file, request);
        if (fileName != null) {
            User user = (User)request.getSession().getAttribute("USER");
            Employee employee = new Employee();
            employee.setId(user.getId());
            employee.setIcon(fileName);
            if (svc.updateIcon(employee) > 0) {
                view.addStaticAttribute("icon", "1");
                view.addStaticAttribute("msg", "文件上传成功！");
                return new ModelAndView(view);
            }

        }
        view.addStaticAttribute("icon", "2");
        view.addStaticAttribute("msg", "文件上传失败！");
        return new ModelAndView(view);
    }

}
